/**
 *
 */
package com.googlecode.bip.entity;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

/**
 *
 * @author Claude CHOISNET
 */
public class Question
    implements Serializable, IQuestion//, Comparable<Question>
{
    private static final long serialVersionUID = 1L;

    private int idquestion;
    private String idlocal;
    private String name;
    private String description;
    private int parentidtopic;
    private int order;
    private int minvalue;
    private int maxvalue;
    private int defaultvalue;
    private int isactive;

    /** Optional value */
    private Integer iduser;
    /** Optional value */
    private Integer uservalue;
    /** Optional value */
    private Timestamp userdate;

    /** Optional value */
    private String usercomment;
    /** Optional value */
    private String valuedescription;

    /**
     * Create a NOT initialized Question
     */
    public Question()
    {
    }

    /**
     * Create a shadow copy of giving Question
     *
     * @param o Question to clone
     */
    public Question( final Question o )
    {
        this.idquestion         = o.idquestion;
        this.idlocal            = o.idlocal;
        this.name               = o.name;
        this.description        = o.description;
        this.parentidtopic      = o.parentidtopic;
        this.order              = o.order;
        this.minvalue           = o.minvalue;
        this.maxvalue           = o.maxvalue;
        this.defaultvalue       = o.defaultvalue;
        this.isactive           = o.isactive;

        /** Optional values */
        this.iduser             = o.iduser;
        this.uservalue          = o.uservalue;
        this.userdate           = o.userdate;
        this.usercomment        = o.usercomment;
        this.valuedescription   = o.valuedescription;
    }

//    @Override
//    public int compareTo( final Question o )
//    {
//        if( o == null ) {
//            return Integer.MIN_VALUE;
//            }
//        if( this == o ) {
//            return 0;
//            }
//
//        int cmp = this.idquestion - o.idquestion;
//        if( cmp != 0 ) { return cmp; }
//        cmp = StringComparator.compareString( this.idlocal, o.idlocal );
//        if( cmp != 0 ) { return cmp; }
//        cmp = StringComparator.compareString( this.name, o.name );
//        if( cmp != 0 ) { return cmp; }
//        cmp = StringComparator.compareString( this.description, o.description );
//        if( cmp != 0 ) { return cmp; }
//        cmp = this.parentidtopic - o.parentidtopic;
//        if( cmp != 0 ) { return cmp; }
//        cmp = this.order - o.order;
//        if( cmp != 0 ) { return cmp; }
//        cmp = this.minvalue - o.minvalue;
//        if( cmp != 0 ) { return cmp; }
//        cmp = this.maxvalue - o.maxvalue;
//        if( cmp != 0 ) { return cmp; }
//        cmp = this.defaultvalue - o.defaultvalue;
//        if( cmp != 0 ) { return cmp; }
//        cmp = this.isactive - o.isactive;
//        if( cmp != 0 ) { return cmp; }
//
//        //
//        // Optional values
//        //
//        cmp = IntegerComparator.compareInteger( this.iduser, o.iduser );
//        if( cmp != 0 ) { return cmp; }
//
//        if( iduser == null ) {
//            return 0;
//            }
//        cmp = this.uservalue - o.uservalue; // should be not null
//        if( cmp != 0 ) { return cmp; }
//        cmp = this.userdate.compareTo( o.userdate ); // should be not null
//        if( cmp != 0 ) { return cmp; }
//        cmp = StringComparator.compareString( this.usercomment, o.usercomment );
//        if( cmp != 0 ) { return cmp; }
//        cmp = StringComparator.compareString( this.valuedescription, o.valuedescription );
//        if( cmp != 0 ) { return cmp; }
//
//        return cmp;
//    }

    public IQuestion set( final ResultSet rSet ) throws SQLException
    {
        idquestion      = rSet.getInt( "idquestion" );
        idlocal         = rSet.getString( "idlocal" );
        name            = rSet.getString( "questionname" );
        description     = rSet.getString( "questiondescription" );
        parentidtopic   = rSet.getInt( "idtopic" );
        order           = rSet.getInt( "order" );
        minvalue        = rSet.getInt( "minvalue" );
        maxvalue        = rSet.getInt( "maxvalue" );
        defaultvalue    = rSet.getInt( "defaultvalue" );
        isactive        =  rSet.getInt( "isactive" );

        try {
            iduser = rSet.getInt( "iduser" );
            }
        catch( SQLException e ) {
            iduser = null;
            }
        if( iduser != null ) {
            // Found 'iduser', so get informations for this value
            uservalue           = rSet.getInt( "uservalue" );
            usercomment         = rSet.getString( "usercomment" );
            valuedescription    = rSet.getString( "valuedescription" );
            userdate            = rSet.getTimestamp( "userdate" );
            }
        else {
            uservalue = null;
            usercomment = valuedescription = null;
            userdate = null;
        }

        return this;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getIdquestion()
     */
    @Override
    public int getIdQuestion()
    {
        return idquestion;
    }

    /**
    * @param idquestion the idquestion to set
    */
    public void setIdQuestion(int idquestion)
    {
        this.idquestion = idquestion;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getIdlocal()
     */
    @Override
    public String getIdLocal()
    {
        return idlocal;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#setIdlocal(java.lang.String)
     */
    @Override
    public void setIdLocal(String idlocal)
    {
        this.idlocal = idlocal;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getName()
     */
    @Override
    public String getName()
    {
        return name;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#setName(java.lang.String)
     */
    @Override
    public void setName(String name)
    {
        this.name = name;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getDescription()
     */
    @Override
    public String getDescription()
    {
        return description;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#setDescription(java.lang.String)
     */
    @Override
    public void setDescription(String description)
    {
        this.description = description;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getParentIdTopic()
     */
    @Override
    public int getParentIdTopic()
    {
        return parentidtopic;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#setParentIdTopic(int)
     */
    @Override
    public void setParentIdTopic(int parentidtopic)
    {
        this.parentidtopic = parentidtopic;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getOrder()
     */
    @Override
    public int getOrder()
    {
        return order;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#setOrder(int)
     */
    @Override
    public void setOrder(int order)
    {
        this.order = order;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getMinValue()
     */
    @Override
    public int getMinValue()
    {
        return minvalue;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#setMinValue(int)
     */
    @Override
    public void setMinValue(int minvalue)
    {
        this.minvalue = minvalue;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getMaxValue()
     */
    @Override
    public int getMaxValue()
    {
        return maxvalue;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#setMaxValue(int)
     */
    @Override
    public void setMaxValue(int maxvalue)
    {
        this.maxvalue = maxvalue;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getDefaultValue()
     */
    @Override
    public int getDefaultValue()
    {
        return defaultvalue;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#setDefaultValue(int)
     */
    @Override
    public void setDefaultValue(int defaultvalue)
    {
        this.defaultvalue = defaultvalue;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getIsActive()
     */
    @Override
    public int getIsActive()
    {
        return isactive;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#setIsActive(int)
     */
    @Override
    public void setIsActive(int isactive)
    {
        this.isactive = isactive;
    }

    // ----------------------------------------------------------
    // Optional
    // ----------------------------------------------------------

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getIdUser()
     */
    @Override
    public Integer getIdUser()
    {
        return iduser;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#setIdUser(java.lang.Integer)
     */
    @Override
    public void setIdUser(Integer iduser)
    {
        this.iduser = iduser;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getUserValue()
     */
    @Override
    public Integer getUserValue()
    {
        return uservalue;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#setUserValue(java.lang.Integer)
     */
    @Override
    public void setUserValue(Integer uservalue)
    {
        this.uservalue = uservalue;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getUserDate()
     */
    @Override
    public Timestamp getUserDate()
    {
        return userdate;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#setUserDate(java.sql.Timestamp)
     */
    @Override
    public void setUserDate(Timestamp userdate)
    {
        this.userdate = userdate;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getUserComment()
     */
    @Override
    public String getUserComment()
    {
        return usercomment;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#setUsercomment(java.lang.String)
     */
    @Override
    public void setUserComment(String usercomment)
    {
        this.usercomment = usercomment;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#getValueDescription()
     */
    @Override
    public String getValueDescription()
    {
        return valuedescription;
    }

    /* (non-Javadoc)
     * @see com.googlecode.bip.entity.IQuestion#setValueDescription(java.lang.String)
     */
    @Override
    public void setValueDescription(String valuedescription)
    {
        this.valuedescription = valuedescription;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString()
    {
        StringBuilder builder = new StringBuilder();

        builder.append("Question [idquestion=").append(idquestion)
               .append(", idlocal=").append(idlocal)
               .append(", name=").append(name)
               .append(", description=").append(description)
               .append(", parentidtopic=").append(parentidtopic)
               .append(", order=").append(order)
               .append(", minvalue=").append(minvalue)
               .append(", maxvalue=").append(maxvalue)
               .append(", defaultvalue=").append(defaultvalue)
               .append(", isactive=").append(isactive)
               .append(", iduser=").append(iduser)
               .append(", uservalue=").append(uservalue)
               .append(", userdate=").append(userdate)
               .append(", usercomment=").append(usercomment)
               .append(", valuedescription=").append(valuedescription)
               .append("]");

        return builder.toString();
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + defaultvalue;
        result = prime * result
                + ((description == null) ? 0 : description.hashCode());
        result = prime * result + ((idlocal == null) ? 0 : idlocal.hashCode());
        result = prime * result + idquestion;
        result = prime * result + ((iduser == null) ? 0 : iduser.hashCode());
        result = prime * result + isactive;
        result = prime * result + maxvalue;
        result = prime * result + minvalue;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + order;
        result = prime * result + parentidtopic;
        result = prime * result
                + ((usercomment == null) ? 0 : usercomment.hashCode());
        result = prime * result
                + ((userdate == null) ? 0 : userdate.hashCode());
        result = prime * result
                + ((uservalue == null) ? 0 : uservalue.hashCode());
        result = prime
                * result
                + ((valuedescription == null) ? 0 : valuedescription.hashCode());
        return result;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals( Object obj )
    {
        if( this == obj ) return true;
        if( obj == null ) return false;
        if( getClass() != obj.getClass() ) return false;
        Question other = (Question)obj;
        if( defaultvalue != other.defaultvalue ) return false;
        if( description == null ) {
            if( other.description != null ) return false;
        } else if( !description.equals( other.description ) ) return false;
        if( idlocal == null ) {
            if( other.idlocal != null ) return false;
        } else if( !idlocal.equals( other.idlocal ) ) return false;
        if( idquestion != other.idquestion ) return false;
        if( iduser == null ) {
            if( other.iduser != null ) return false;
        } else if( !iduser.equals( other.iduser ) ) return false;
        if( isactive != other.isactive ) return false;
        if( maxvalue != other.maxvalue ) return false;
        if( minvalue != other.minvalue ) return false;
        if( name == null ) {
            if( other.name != null ) return false;
        } else if( !name.equals( other.name ) ) return false;
        if( order != other.order ) return false;
        if( parentidtopic != other.parentidtopic ) return false;
        if( usercomment == null ) {
            if( other.usercomment != null ) return false;
        } else if( !usercomment.equals( other.usercomment ) ) return false;
        if( userdate == null ) {
            if( other.userdate != null ) return false;
        } else if( !userdate.equals( other.userdate ) ) return false;
        if( uservalue == null ) {
            if( other.uservalue != null ) return false;
        } else if( !uservalue.equals( other.uservalue ) ) return false;
        if( valuedescription == null ) {
            if( other.valuedescription != null ) return false;
        } else if( !valuedescription.equals( other.valuedescription ) )
            return false;
        return true;
    }

}
